package com.kzd.gtboon.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtboon.base.BaseViewModel
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.jetpack.api.*
import com.module.lemlin.http.*
import com.module.lemlin.owner.setRxLifeScope

class TravelViewModel(application: Application) : BaseViewModel(application) {

    private var page: Int = 1
    private var limit: Int = 20

    private var start = 0L

    val travelTodayLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<Travel<GameGroup>>>>()
    fun travelToday(isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<Travel<GameGroup>>>(travelTodayLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<Travel<GameGroup>>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                start = resp.data.start
            }
        }) {
            if (!isLoadMore) {
                start = 0
            }
            newStarterList(start.toString())
        }
    }

    val travelWeekLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun travelWeek() {
        setRxLifeScope(SubscribeViewModel(travelWeekLiveData)) {
            newStarterWeekList()
        }
    }

    val travelReserveLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<Travel<GameReserve>>>>()
    fun travelReserve(isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<Travel<GameReserve>>>(travelReserveLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<Travel<GameReserve>>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                start = resp.data.start
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
                start = 0
            }
            newStarterReserve(start.toString(), page.toString(), limit.toString())
        }
    }

    val reserveChangeLiveData = MutableLiveData<HttpResponseBody<ReserveChangeResponse>>()
    fun reserveChange(gid: String, type: String,position:Int) {
        setRxLifeScope(object :SubscribeViewModel<ReserveChangeResponse>(reserveChangeLiveData){
            override fun onSuccess(resp: ReserveChangeResponse) {
                super.onSuccess(resp.apply { this.position = position })
            }
        }) {
            newStarterReserveChange(gid, type)
        }
    }

}